<?php
// http://app.com/list.php?page = 1&pagesize =12
require_once ('filecache.php');
require_once ('jsonAndxml.php');
require_once ('db.php');
$page = isset($_GET['page']) ? $_GET['page'] : 1;
$pageSize = isset($_GET['pagesize']) ? $_GET['pagesize'] : 6;
if (! is_numeric($page) || ! is_numeric($pageSize)) {
    return Response::show(401, '数据不合法');
}

$offset = ($page - 1) * $pageSize; // 分页偏移量
$sql = "select * from imooc_admin  ORDER BY username desc limit " . $offset . "," . $pageSize;

$cache = new File();
$admins = array();
if (! $admins = $cache->cacheData('index_mk_cache' . $page . '-' . $pageSize)) {
    //echo 1;exit;测试缓存失效
    //如果缓存存在，就把值赋给admins,就跳过数据库的操作，直接将缓存的值返回出来
    //不存在就执行数据库里面，并生成缓存
    try {
        
        $connect = DB::getInstance()->connect();
    } catch (Exception $e) {
        
        return Response::show(403, '数据库连接失败');
    }
    $result = mysql_query($sql, $connect);
    
    while ($admin = mysql_fetch_assoc($result)) { // mysql_fetch_assoc获取的结果 字段用key表示，字段值用value
        $admins[] = $admin;
    }
    
    if ($admins){
        //生成缓存
        $cache->cacheData('index_mk_cache' . $page . '-' . $pageSize,$admins,1200);
    }
}

if ($admins) { // 数据存在
    return Response::show(200, '首页数据获取成', $admins);
} else {
    return Response::show(400, '首页数据获取成', $admins);
}

